<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    //调用登录方法
                    this.$http.get(this.ajxaHttpUrl+"Login/Login",
                    {
                        params:{
                            username:this.form.userName,
                            password:this.form.password,
                        }
                    })
                    .then((res) => {
                        if(res.data.status==1){
                            this.$Message.success("登录成功")
                            Cookies.set('user', this.form.userName);//存名字
                            Cookies.set('password', this.form.password);//存密码
                            Cookies.set('token', res.data.token)
                            Cookies.set('access', 0);//存权限
                            this.$store.commit('setAvator', "http://img3.imgtn.bdimg.com/it/u=4252777213,3972425634&fm=26&gp=0.jpg");//存头像
                            this.$router.push({ name: 'home_index' });//跳到首页
                        }else{
                            this.$Message.error(res.data.msg||res.data.info);
                        }
                    })
                    .catch((err) => {
                        this.$Message.error(err);
                    });
                }
            });
        }
    }
};
</script>

<style>

</style>
